package net.shopxx.entity;

import java.math.BigDecimal;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.shopxx.util.SystemConfigUtil;

/**
 * 实体类 - 购物车项
 */

@Entity
@DiscriminatorValue(value="g")
public class CartItemGroupbuy extends CartItem {

	private static final long serialVersionUID = -4241469437632553865L;
	
	private Groupbuy groupbuy;// 商品
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false)
	public Groupbuy getGroupbuy() {
		return groupbuy;
	}

	public void setGroupbuy(Groupbuy groupbuy) {
		this.groupbuy = groupbuy;
	}

	// 获取优惠价格，若member对象为null则返回原价格
	@Transient
	public BigDecimal getPreferentialPrice() {
		if (member != null) {
			BigDecimal preferentialPrice = SystemConfigUtil.calculate(groupbuy.getPrice(), new Double(member.getMemberRank().getPreferentialScale().toString()));
			return SystemConfigUtil.getPriceScaleBigDecimal(preferentialPrice);
		} else {
			return new BigDecimal(groupbuy.getPrice());
		}
	}
	
	// 获取小计价格
	@Transient
	public BigDecimal getSubtotalPrice() {
		BigDecimal subtotalPrice = getPreferentialPrice().multiply(new BigDecimal(quantity.toString()));
		return SystemConfigUtil.getOrderScaleBigDecimal(subtotalPrice);
	}

}